﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WxPackage.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WxPackage.cs
// 创建时间：2014-12-25 12:58
// 修改时间：2014-12-23 15:47
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

namespace Payment.Weixin
{
    #region

    using System;
    using System.Collections.Generic;

    #endregion

    /// <summary>
    /// The wx package.
    /// </summary>
    public class WxPackage : Dictionary<string, string>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WxPackage"/> class.
        /// </summary>
        /// <param name="payOrder">
        /// The pay Service.
        /// </param>
        /// <param name="wxConfig">
        /// The wx config.
        /// </param>
        public WxPackage(IPayOrder payOrder, WxConfig wxConfig)
        {
            this.Partner = wxConfig.Partner;
            this.PartnerKey = wxConfig.PartnerKey;
            this.InputCharset = "UTF-8";
            this.BankType = "WX";
            this.FeeType = "1";
            this.PayOrder = payOrder;
            this.OutTradeNo = payOrder.GetId();
            this.TotalFee = payOrder.GetTotalFeeInt().ToString(); ;
            this.Body = payOrder.GetSubject(); 
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the attach.
        /// </summary>
        public string Attach
        {
            get
            {
                return this["attach"];
            }

            set
            {
                this["attach"] = value;
            }
        }

        /// <summary>
        /// Gets the bank type.
        /// </summary>
        public string BankType
        {
            get
            {
                return this["bank_type"];
            }

            private set
            {
                this["bank_type"] = value;
            }
        }

        /// <summary>
        /// Gets the body.
        /// </summary>
        public string Body
        {
            get
            {
                return this["body"];
            }

            private set
            {
                this["body"] = value;
            }
        }

        /// <summary>
        /// Gets the fee type.
        /// </summary>
        public string FeeType
        {
            get
            {
                return this["fee_type"];
            }

            private set
            {
                this["fee_type"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the input charset.
        /// </summary>
        public string InputCharset
        {
            get
            {
                return this["input_charset"];
            }

            set
            {
                this["input_charset"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the notify url.
        /// </summary>
        public string NotifyUrl
        {
            get
            {
                return this["notify_url"];
            }

            set
            {
                this["notify_url"] = value;
            }
        }

        /// <summary>
        /// Gets the out trade no.
        /// </summary>
        public string OutTradeNo
        {
            get
            {
                return this["out_trade_no"];
            }

            private set
            {
                this["out_trade_no"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the partner.
        /// </summary>
        public string Partner
        {
            get
            {
                return this["partner"];
            }

            set
            {
                this["partner"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the partner key.
        /// </summary>
        public string PartnerKey { get; protected set; }

        /// <summary>
        /// Gets or sets the pay Service.
        /// </summary>
        public IPayOrder PayOrder { get; set; }

        /// <summary>
        /// Gets or sets the spbill create ip.
        /// </summary>
        public string SpbillCreateIp
        {
            get
            {
                return this["spbill_create_ip"];
            }

            set
            {
                this["spbill_create_ip"] = value;
            }
        }

        /// <summary>
        /// Gets the total fee.
        /// </summary>
        public string TotalFee
        {
            get
            {
                return this["total_fee"];
            }

            private set
            {
                this["total_fee"] = value;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get package.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetPackage()
        {
            this.Va();
            string orderby = WxCommon.GetOrderBy(this);
            string stringSignTemp = WxCommon.GetStringSignTemp(orderby, this.PartnerKey);
            string urlencode = WxCommon.GetUrlencode(this);
            return urlencode + "&sign=" + stringSignTemp;
        }

        #endregion

        #region Methods

        /// <summary>
        /// 验证数据是否通过
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private bool Va()
        {
            foreach (var item in this)
            {
                if (string.IsNullOrWhiteSpace(item.Value))
                {
                    throw new Exception(item.Key + "为空");
                }
            }

            if (string.IsNullOrWhiteSpace(this.PartnerKey))
            {
                throw new Exception("PaternerKey为空");
            }

            return true;
        }

        #endregion
    }
}